﻿using HelixToolkit.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace QuickStart_Project_01.Model3Ds
{
    public class SiloModle3D : Visual3Dobjet
    {
        GeometryModel3D body = new GeometryModel3D();
        GeometryModel3D railing = new GeometryModel3D();
        GeometryModel3D stairs = new GeometryModel3D();

        public SiloModle3D()
        {
            body.Material = MaterialHelper.CreateMaterial(Colors.LightGray, 0.5);
            railing.Material = MaterialHelper.CreateMaterial(Colors.Orange, 0.5);
            stairs.Material = MaterialHelper.CreateMaterial(Colors.Brown, 0.5);

            this.InitModel();

            Model3DGroup group = new Model3DGroup();
            group.Children.Add(railing);
            group.Children.Add(body);
            group.Children.Add(stairs);
            this.Visual3DModel = group;


            //this.Datas = new List<Models.MonitorItemModel>();
            //this.Datas.Add(new Models.MonitorItemModel { Name = "实时温度",Unit="℃" });
            //this.Datas.Add(new Models.MonitorItemModel { Name = "实时湿度",Unit="%" });
            //this.Datas.Add(new Models.MonitorItemModel { Name = "监测变量",Unit="%" });
            //this.Datas.Add(new Models.MonitorItemModel { Name = "实时湿度",Unit="%" });
            //this.Datas.Add(new Models.MonitorItemModel { Name = "实时湿度",Unit="%" });
            //this.Datas.Add(new Models.MonitorItemModel { Name = "实时湿度",Unit="%" });
            //this.Datas.Add(new Models.MonitorItemModel { Name = "实时湿度",Unit="%" });
            //this.Datas.Add(new Models.MonitorItemModel { Name = "实时湿度",Unit="%" });
            //this.Datas.Add(new Models.MonitorItemModel { Name = "实时湿度",Unit="%" });
        }

        public override void Selected(bool selected)
        {
            double opacity = 0.5;
            if (selected)
            {
                opacity = 1;
            }
            body.Material = MaterialHelper.CreateMaterial(Colors.LightGray, opacity);
            railing.Material = MaterialHelper.CreateMaterial(Colors.Orange, opacity);   // zhalan
            stairs.Material = MaterialHelper.CreateMaterial(Colors.Brown, opacity);     // louti
        }


        // 高度、直径、圆顶高度
        public double Height
        {
            get { return (double)GetValue(HeightProperty); }
            set { SetValue(HeightProperty, value); }
        }
        public static readonly DependencyProperty HeightProperty =
            DependencyProperty.Register("Height",
                typeof(double), typeof(SiloModle3D),
                new PropertyMetadata(20.0, new PropertyChangedCallback((s, e) => (s as SiloModle3D).InitModel())));



        public double Diameter
        {
            get { return (double)GetValue(DiameterProperty); }
            set { SetValue(DiameterProperty, value); }
        }
        public static readonly DependencyProperty DiameterProperty =
            DependencyProperty.Register("Diameter", typeof(double), typeof(SiloModle3D),
                new PropertyMetadata(40.0, new PropertyChangedCallback((s, e) => (s as SiloModle3D).InitModel())));

        public double DomeHeight
        {
            get { return (double)GetValue(DomeHeightProperty); }
            set { SetValue(DomeHeightProperty, value); }
        }
        public static readonly DependencyProperty DomeHeightProperty =
            DependencyProperty.Register("DomeHeight", typeof(double), typeof(SiloModle3D),
                new PropertyMetadata(8.0, new PropertyChangedCallback((s, e) => (s as SiloModle3D).InitModel())));

        public double DomeDiameter
        {
            get { return (double)GetValue(DomeDiameterProperty); }
            set { SetValue(DomeDiameterProperty, value); }
        }
        public static readonly DependencyProperty DomeDiameterProperty =
            DependencyProperty.Register("DomeDiameter", typeof(double), typeof(SiloModle3D),
                new PropertyMetadata(38.0, new PropertyChangedCallback((s, e) => (s as SiloModle3D).InitModel())));



        private void InitModel()
        {
            var body_builder = new MeshBuilder(generateNormals: false, generateTexCoords: false);
            var p0 = new Point(0, 0);
            var p1 = new Point(Diameter / 2, 0);
            var p2 = new Point(Diameter / 2, Height);
            var p3 = new Point(DomeDiameter / 2, Height);

            var section = new List<Point> { p0, p1, p1, p2, p2, p3 };
            var indices = new List<int> { 0, 1, 2, 3, 4, 5 };
            int n = 40;
            for (int i = n; i >= 0; i--)
            {
                double x = (double)i / n;
                double y = x * x;

                if (i < n)
                {
                    indices.Add(section.Count - 1);
                    indices.Add(section.Count);
                }
                section.Add(new Point(x * this.DomeDiameter / 2, this.Height + (this.DomeHeight * (1 - y))));
            }

            body_builder.AddSurfaceOfRevolution(
                new Point3D(0, 0, 0), new Vector3D(0, 0, 1), section,
                indices, 80);
            this.body.Geometry = body_builder.ToMesh();



            // 踏步
            var treadDepth = 0.3;
            var riseHeight = 0.15;
            var thickness = 0.05;
            var width = 1;
            var steps = (int)(this.Height / riseHeight);
            var r = (this.Diameter * 0.5) + (width * 0.5);
            var rp = (this.Diameter * 0.5) + (width * 0.95);
            var stairBuilder = new MeshBuilder(false, false);
            var railBases = new List<Point3D>();
            for (int i = 0; i < steps; i++)
            {
                var theta = treadDepth * i / r;
                var p = new Point3D(Math.Cos(theta) * r, Math.Sin(theta) * r, (riseHeight * i) + (thickness / 2));
                var x = new Vector3D(Math.Cos(theta), Math.Sin(theta), 0);
                var z = new Vector3D(0, 0, 1);
                var y = Vector3D.CrossProduct(z, x);

                stairBuilder.AddBox(p, x, y, width, treadDepth, thickness);
                railBases.Add(new Point3D(Math.Cos(theta) * rp, Math.Sin(theta) * rp, (riseHeight * i) + thickness));
            }
            this.stairs.Geometry = stairBuilder.ToMesh();



            // 栏杆
            var lastTheta = treadDepth * steps / r;

            // Railing along stairs
            var railingHeight = 0.8;
            var railingDiameter = 0.05;
            var railings = 3;
            var railingBuilder = new MeshBuilder(false, false);

            // Top railing
            var railingPostDistance = 0.5;
            int topRailingPosts = (int)(this.Diameter * Math.PI / railingPostDistance);
            var tr = (this.Diameter / 2) - railingDiameter;
            for (int i = 0; i < topRailingPosts; i++)
            {
                var theta = lastTheta + (2 * Math.PI * i / topRailingPosts);
                railBases.Add(new Point3D(Math.Cos(theta) * tr, Math.Sin(theta) * tr, this.Height));
            }

            BuildRailing(railingBuilder, railBases, railingHeight, railingDiameter, railings);

            this.railing.Geometry = railingBuilder.ToMesh();
        }

        private void BuildRailing(
            MeshBuilder railingBuilder,
            List<Point3D> bases,
            double height,
            double diameter,
            int railings)
        {
            foreach (var point in bases)
            {
                railingBuilder.AddCylinder(point, point + new Vector3D(0, 0, height), diameter, 10);
            }

            for (int i = 1; i <= railings; i++)
            {
                var h = height * i / railings;
                var path = bases.Select(p => p + new Vector3D(0, 0, h)).ToArray();
                railingBuilder.AddTube(path, diameter, 10, false);
            }
        }
    }
}
